#include <cassert>

#include "Application.h"
#include "VMRPlayerViewController.h"
#include "VMRPlayerScrollView.h"
#include "VMRPlayer.h"
#include "VMRPlayerView.h"
#include "DX.h"
#include "DXButtonView.h"
#include "DXImage.h"
#include "Atoms.h"
#include "NCenter.h"
#include "Window.h"

VMRPlayerViewController::VMRPlayerViewController( MRect&rect, std::wstring fileURL ) : ViewController( 0 ), 
																						_vmrPlayer( 0 ),
																						_backButton( 0 ),
																						delegate( 0 )
{
	try
	{
		_vmrPlayer = new VMRPlayer( fileURL, false, true );
		_view = new VMRPlayerView( rect, _vmrPlayer );
		
		ViewController::view( _view );

		_imageManager.add( TEXT( "controlData\\Aback.png" ), new DXImage( TEXT( "controlData\\Aback.png" ) ) );
		_imageManager.add( TEXT( "controlData\\Aback_LIGHT.png" ), new DXImage( TEXT( "controlData\\Aback_LIGHT.png" ) ) );
		_imageManager.add( TEXT( "controlData\\vmrScrollBack.png" ), new DXImage( TEXT( "controlData\\vmrScrollBack.png" ) ) );
		_imageManager.add( TEXT( "controlData\\vmrScroll.png" ), new DXImage( TEXT( "controlData\\vmrScroll.png" ) ) );

		float butSize = 49.0f;

		// back
		_backButton = new DXButtonView( MRect( 30.0f, 30.0f, butSize, butSize ), 
										_imageManager.get( TEXT( "controlData\\Aback.png" ) ), 
										_imageManager.get( TEXT( "controlData\\Aback_LIGHT.png" ) ), 
										TEXT( "backButtonVideo" ) );
		//_backButton->leftMouseDownObservers.addObserver( this, &VMRPlayerViewController::exit );
		_backButton->pressAction = new Observer< VMRPlayerViewController >( this, &VMRPlayerViewController::exit );

		_view->addSubView( _backButton );

		// scroll
		_scrollView = new VMRPlayerScrollView( MRect( 
													//_view->rect().x() + 100.0f - butSize - 400, 
													- 100.0f,
													_view->rect().y() + _view->rect().height() - 100.0f - 70.0f, 
													2062,
													217
													//_view->rect().width() - 200.0f, 
													//100.0f 
												),
												_imageManager.get( TEXT( "controlData\\vmrScrollBack.png" ) ), 
												_imageManager.get( TEXT( "controlData\\vmrScroll.png" ) ), 
												_vmrPlayer );

		_scrollView->delegate( this );
		_view->addSubView( _scrollView );
	}
	catch ( std::wstring error )
	{
		MessageBox( 0, ( fileURL + std::wstring( TEXT( " file can't read" ) ) ).c_str(), TEXT( "error" ) , MB_OK );
	}
}

VMRPlayerViewController::~VMRPlayerViewController( )
{
	view()->removeSubView( _backButton );
	view()->removeSubView( _scrollView );

	if ( View* super = view()->superView() ) super->removeSubView( view() );
	
	delete _backButton;
	delete _scrollView;

	
	delegate = 0;
	delete _vmrPlayer;
}

/*VMRPlayer*/View* VMRPlayerViewController::view()
{
	//return ( VMRPlayerView* )__super::view();
	return _view;
}

void VMRPlayerViewController::play( IParam* param )
{
	assert( _vmrPlayer );

	_vmrPlayer->play();
}

void VMRPlayerViewController::pause( IParam* param )
{
	assert( _vmrPlayer );

	_vmrPlayer->pause();
}

void VMRPlayerViewController::exit( IParam* param )
{
	//assert( delegate );

	_vmrPlayer->pause();
	
	//delegate->didCloseVideo( 0 );
	Application::_()->nCenter->post( TEXT( "didCloseVideo" ), 0 );
}

VMRPlayer* VMRPlayerViewController::vmrPlayer()
{
	return _vmrPlayer;
}

// VMRPlayerScrollViewDelegate methods
void VMRPlayerViewController::scrollChangedPosition( void* param )
{
	LONGLONG* pos = ( LONGLONG* )param;
	_vmrPlayer->position( *pos );
	if ( !_vmrPlayer->isVideoPlaying() )
	{
		playButtonPressed( 0 );
	}
}

void VMRPlayerViewController::playButtonPressed( void* param )
{
	assert( _vmrPlayer );

	play( 0 );
}

void VMRPlayerViewController::pauseButtonPressed( void* param )
{
	assert( _vmrPlayer );

	pause( 0 );
}